#include "translatewindow.h"
#include "ui_translatewindow.h"
#include "googleapi.h"

TranslateWindow::TranslateWindow(QWidget *parent, QString text) :
    QDialog(parent),
    ui(new Ui::TranslateWindow)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::SplashScreen);
    ui->engExprCmdButton->setText(text);
    enExpressionString = text;
    connect(ui->engExprCmdButton, SIGNAL(clicked()), this, SLOT(speechText()));
    QTimer::singleShot(10, this, SLOT(translateText()));
}

TranslateWindow::~TranslateWindow()
{
    delete ui;
}

void TranslateWindow::speechText()
{
    GoogleApi api;
    api.texttospeech("en", enExpressionString);
}

void TranslateWindow::translateText()
{
    GoogleApi api;
    QString translated = api.translate("en", "hu", enExpressionString);
    if (translated.isEmpty()) {
        ui->hunTextLabel->setText(ui->hunTextLabel->text() + " ...>");
        translated = api.translate("en", "hu", enExpressionString);
    }
    if (translated == enExpressionString)
        ui->hunTextLabel->setText("[" + enExpressionString + "]\n" + tr("*not found!*"));
    else if (translated.isEmpty())
        ui->hunTextLabel->setText(tr("*Google server not response!*"));
    else
        ui->hunTextLabel->setText("<b>" + translated + "</b>");
}
